/**
 * 
 */
package com.magnusart.scrumify.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.magnusart.scrumppm.model.Project;
import com.magnusart.scrumppm.model.Story;
import com.magnusart.scrumppm.model.StoryBuilder;
import com.magnusart.scrumppm.model.Story.Units;

/**
 * @author Magnus Andersson
 *
 */
final public class HsqlDbStoryDAO extends HsqlDbAbstactDAO<Project, Story, StoryBuilder>
{
	
	public HsqlDbStoryDAO( HsqlDbDAOFactory factory ) {
		super( factory );
	}
	
	{
		sqlAdd = "INSERT INTO Stories( " +
		"projectName, " +
		"storyTitle, " +
		"storyDescription, " +
		"storyDeliverable, " +
		"storyPriority, " +
		"skipStory, " +
		"isDone, " +
		"estimatedUnits ) " + 
		"VALUES( ?, ?, ?, ?, ?, ?, ?, ? )";
		
		sqlGetAll = "SELECT storyID, " +
		"storyTitle, " +
		"storyDescription, " +
		"storyDeliverable, " +
		"storyPriority, " +
		"skipStory, " +
		"isDone, " +
		"estimatedUnits FROM Stories WHERE projectName = ?";
		
		sqlGetOne = "SELECT storyID, " +
		"storyTitle, " +
		"storyDescription, " +
		"storyDeliverable, " +
		"storyPriority, " +
		"skipStory, " +
		"isDone, " +
		"estimatedUnits FROM Stories WHERE storyID = ?";
		
		sqlUpdate = "UPDATE Stories SET " +
		"projectName = ?, " +
		"storyTitle = ?,  " +
		"storyDescription = ?,  " +
		"storyDeliverable = ?,  " +
		"storyPriority = ?,  " +
		"skipStory = ?,  " +
		"isDone = ?,  " +
		"estimatedUnits = ? WHERE storyID = ? ";
		
		sqlDelete = "DELETE FROM Stories WHERE storyID = ?";
	}

	@Override
	void perpareAdd(Project parent, Story entry, PreparedStatement pstmt ) throws SQLException {
		// Add values
		pstmt.setString( 	1, parent.getName() 					);
		pstmt.setString( 	2, entry.getTitle() 					);
		pstmt.setString( 	3, entry.getDescription() 				);
		pstmt.setString(	4, entry.getDeliverable() 				);
		pstmt.setInt( 		5, entry.getPriority() 					);
		pstmt.setBoolean( 	6, entry.skip() 						);
		pstmt.setBoolean( 	7, entry.isDone() 						);
		pstmt.setInt( 		8, entry.getEstimatedUnits().value() 	);	
	}

	@Override
	void prepareDelete( Story entry,  PreparedStatement pstmt  ) throws SQLException {
		pstmt.setInt( 1, entry.getID() );
	}

	@Override
	void prepareGetAll( Project parent,  PreparedStatement pstmt ) throws SQLException {
		pstmt.setString( 1, parent.getName() );
	}
	

	@Override
	Story pepareUpdate( Project parent, Story entry, StoryBuilder builder,  PreparedStatement pstmt )
			throws SQLException {
		Story updated = new Story( builder );
		
		// Add values
		pstmt.setString( 	1, parent.getName() 					);
		pstmt.setString( 	2, updated.getTitle()					);
		pstmt.setString( 	3, updated.getDescription() 			);
		pstmt.setString(	4, updated.getDeliverable() 			);
		pstmt.setInt( 		5, updated.getPriority() 				);
		pstmt.setBoolean( 	6, updated.skip() 						);
		pstmt.setBoolean( 	7, updated.isDone() 					);
		pstmt.setInt( 		8, updated.getEstimatedUnits().value() 	);
		pstmt.setInt( 		9, entry.getID()						);
		
		return updated;
	}

	@Override
	void prepareGetOne( Story entry,  PreparedStatement pstmt  ) throws SQLException {
		pstmt.setInt( 1, entry.getID() );
	}

	@Override
	Story fetchBuilderData( ResultSet rs, StoryBuilder builder ) throws SQLException {
		builder = new StoryBuilder();
		builder.setID( rs.getInt( "storyID" ) );
		builder.setTitle( rs.getString( "storyTitle" ) );
		builder.setDescription( rs.getString( "storyDescription" ) );
		builder.setDeliverable( rs.getString( "storyDeliverable" ) );
		builder.setPriority( rs.getInt( "storyPriority" ) );
		builder.setSkip( rs.getBoolean( "skipStory" ) );
		builder.setDone( rs.getBoolean( "isDone" ) );
		builder.setEstimatedUnits( convertToUnit( rs.getInt( "estimatedUnits" ) ) );
		
		return new Story( builder );
	}
	
	private Units convertToUnit( int unit) {
		switch( unit ) {
		case -1:
			return Units.UNSPECIFIED;
		case 1:
			return Units.ONE;
		case 2:
			return Units.TWO;
		case 3:
			return Units.THREE;
		case 5:
			return Units.FIVE;
		case 8:
			return Units.EIGHT;
		case 13:
			return Units.THIRTEEN;
		default:
			throw new IllegalArgumentException("Unit out of bounds. Needs to be -1 or 1,2,3,5,8,13.");
		}
	}

	@Override
	Story addedEntry( DAOFactory factory, Story entry, StoryBuilder builder, int addedID) {
		builder = new StoryBuilder( entry );
		builder.setID( addedID );
		
		return new Story( builder );
	}


	
}
